<template>
  <div
    v-empty="nopermission"
    class="rc-cont"
    xs-empty-icon="nopermission"
    xs-empty-text="暂无权限">
    <flexbox
      v-if="showEdit"
      class="rc-head"
      direction="row-reverse">
      <el-button
        class="rc-head-item"
        type="primary"
        @click.native="createClick">新建</el-button>
      <el-button
        v-if="canRelation"
        class="rc-head-item"
        type="primary"
        @click.native="deleteHandleClick">删除</el-button>
      <!-- <el-popover
        v-if="canRelation"
        v-model="showRelativeView"
        placement="bottom"
        width="700"
        popper-class="no-padding-popover"
        trigger="click"
        style="margin-right: 20px;">
        <crm-relative
          ref="crmrelative"
          v-model="showRelativeView"
          :show="showRelativeView"
          :radio="false"
          :action="{ type: 'condition', data: { moduleType: 'customer', customerId: customerId } }"
          :selected-data="{ 'contacts': list }"
          crm-type="contacts"
          @close="showRelativeView = false"
          @changeCheckout="checkRelativeInfos"/>
        <el-button
          slot="reference"
          class="rc-head-item"
          style="margin-right: 0;"
          type="primary"
          @click.native="showRelativeView = true">关联</el-button>
      </el-popover> -->

    </flexbox>
    <el-table
      :data="list"
      height="calc(100% - 45px)"
      :header-cell-style="headerRowStyle"
      :cell-style="cellStyle"
      stripe
      style="width: 100%;border: 1px solid #E6E6E6;"
      @row-click="handleRowClick"
      @selection-change="selectionList = $event">
      <el-table-column
        v-if="canRelation && fieldList.length > 0"
        show-overflow-tooltip
        :selectable="checkSelectPreSales"
        key="0"
        type="selection"
        align="center"
        width="55"/>
      <el-table-column
        v-for="(item, index) in fieldList"
        :key="index+1"
        :prop="item.prop"
        :label="item.label"
        show-overflow-tooltip/>
      <el-table-column
        key="100"
        v-if="fieldList.length > 0"
        show-overflow-tooltip
        label="审核状态"
        width="100"
        fixed="right">
        <template slot-scope="scope">
          <div
            :style="getStatusStyle(scope.row)"
            class="status_button">
            {{ getStatusName(scope.row) }}
          </div>
        </template>
      </el-table-column>
    </el-table>
    <c-r-m-full-screen-detail
      :visible.sync="showFullDetail"
      :id="preSalesId"
      crm-type="preSales"/>
    <c-r-m-create-view
      v-if="isCreate"
      :action="createActionInfo"
      crm-type="preSales"
      @save-success="getDetail"
      @hiden-view="isCreate=false"/>
  </div>
</template>

<script type="text/javascript">
import loading from '../mixins/loading'
import CRMCreateView from './CRMCreateView'
import { crmCustomerQueryContacts } from '@/api/customermanagement/customer'
import {
  // crmBusinessQueryContactsAPI,
  // crmBusinessRelateContactsAPI,
  // crmBusinessUnrelateContactsAPI,
  crmBusinessQueryPreSalesAPI,
  crmBusinessUnrelatePreSalesAPI
} from '@/api/customermanagement/business'
// import CrmRelative from '@/components/CreateCom/CrmRelative'

export default {
  name: 'RelativePreSales', // 相关设备清单列表  可能再很多地方展示 放到客户管理目录下
  components: {
    CRMFullScreenDetail: () => import('./CRMFullScreenDetail.vue'),
    CRMCreateView,
    // CrmRelative
  },
  mixins: [loading],
  props: {
    /** 模块ID */
    id: [String, Number],
    /** 没有值就是全部类型 有值就是当个类型 */
    crmType: {
      type: String,
      default: ''
    },
    /** 是公海 默认是客户 */
    isSeas: {
      type: Boolean,
      default: false
    },
    /** 联系人人下 新建商机 需要联系人里的客户信息  合同下需要客户和商机信息 */
    detail: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      nopermission: false,
      list: [],
      fieldList: [],
      showFullDetail: false,
      isCreate: false, // 控制新建
      preSalesId: '', // 查看全屏联系人详情的 ID
      // 创建的相关信息
      createActionInfo: { type: 'relative', crmType: this.crmType, data: {}},
      /**
       * 关联的逻辑
       */
      // showRelativeView: false, // 控制关联信息视图
      selectionList: [] // 取消关联勾选的数据
    }
  },
  computed: {
    // 联系人下客户id获取关联商机
    // customerId() {
    //   return this.detail.customerId
    // },
    // 是否能关联
    canRelation() {
      return this.crmType == 'business'
    },

    showEdit(){
      if(this.isSeas){
        return false
      }
      // if(this.crmType == 'business' && this.detail.statusId > 1){
      //   return false
      // }
      if(this.detail.checkStatus != 1){
        return false
      }

      return true;
    }
  },
  watch: {
    id: function(val) {
      this.list = []
      this.getDetail()
    }
  },
  mounted() {
    
  },
  activated: function() {
    this.getDetail()
  },
  deactivated: function() {},
  methods: {
    /**
     * 关联的数据
     */
    // checkRelativeInfos(data) {
    //   if (data.data.length > 0) {
    //     const params = { businessId: this.id }
    //     params.contactsIds = data.data
    //       .map(item => {
    //         return item.contactsId
    //       })
    //       .join(',')
    //     crmBusinessRelateContactsAPI(params)
    //       .then(res => {
    //         this.getDetail()
    //         this.$message.success('操作成功')
    //       })
    //       .catch(() => {})
    //   }
    // },
    // 只有撤回或则未提交的可删除
    checkSelectPreSales(row,index){
      // 审核中、待审核、已审核不能删除
      if(row.checkStatus == 0 || row.checkStatus == 1 || row.checkStatus == 3){
        return false
      }
      return true
    },
    /**
     * 删除设备清单
     */
    deleteHandleClick() {
      if (this.selectionList.length == 0) {
        this.$message.error('请先勾选数据')
      } else {
        this.$confirm('确认删除'+ this.selectionList.length +'条设备清单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            const params = { businessId: this.id }
            params.preSalesIds = this.selectionList
              .map(item => {
                return item.preSalesId
              })
              .join(',')
            crmBusinessUnrelatePreSalesAPI(params)
              .then(res => {
                this.getDetail()
                this.$message.success('操作成功')
              })
              .catch(() => {})
          })
          .catch(() => {
            this.$message.info('已取消操作')
          })
      }
    },

    /**
     * 表头数据
     */
    getFieldList() {
      this.fieldList = [];
      this.fieldList.push({ prop: 'fileName', width: 300, label: '设备清单' })
      this.fieldList.push({ prop: 'preSalesExpectTime', width: '200', label: '期望现场评估时间' })
      // this.fieldList.push({ prop: 'preSalesLatestTime', width: '200', label: '最迟时间' })
      this.fieldList.push({ prop: 'ownerUserName', width: '200', label: '负责人' })
      this.fieldList.push({ prop: 'remark', width: '200', label: '备注' })
    },

    /**
     * 获取数据
     */
    getDetail() {
      this.loading = true
      const request = {
        business: crmBusinessQueryPreSalesAPI
      }[this.crmType]
      const params = {}
      params[this.crmType + 'Id'] = this.id
      request(params)
        .then(res => {
          if (this.fieldList.length == 0) {
            this.getFieldList()
          }
          this.nopermission = false
          this.loading = false
          this.list = res.data.list
        })
        .catch(data => {
          if (data.code == 102) {
            this.nopermission = true
          }
          this.loading = false
        })
    },
    // 当某一行被点击时会触发该事件
    handleRowClick(row, column, event) {
      this.preSalesId = row.preSalesId
      this.showFullDetail = true
    },
    /** 通过回调控制表头style */
    headerRowStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    },
    /** 通过回调控制style */
    cellStyle({ row, column, rowIndex, columnIndex }) {
      return { textAlign: 'center' }
    },
    /** 新建 */
    createClick() {
      /** 客户 下新建设备清单 */
      this.createActionInfo.data['customer'] = this.detail
      this.createActionInfo.data['business'] = this.detail
      this.createActionInfo.relativeData = {
        businessId: this.detail.businessId
      }
      this.isCreate = true
    },
    // createSaveSuccess() {
    //   if (this.canRelation) {
    //     this.$refs.crmrelative.refreshList()
    //   } else {
    //     this.getDetail()
    //   }
    // }

    // 0 未审核 1 审核通过 2 审核拒绝 3 审核中 4 已撤回 5草稿 6 作废 修正 以 getStatusName 为准
    getStatusStyle(row) {
      const status = row.checkStatus;
      const is_accept = row.isAccept;
      if (status == 0) {
        return {
          'border-color': '#E6A23C',
          'background-color': '#FDF6EC',
          'color': '#E6A23C'
        }
      } else if (status == 3) {
        return {
          'border-color': '#409EFF',
          'background-color': '#ECF5FF',
          'color': '#409EFF'
        }
      } else if (status == 1) {
        // if(is_accept == 1){
        //   return {
        //     'border-color': '#67C23A',
        //     'background-color': '#67C23A',
        //     'color': '#FFF'
        //   }
        // }else {
        //   return {
        //     'border-color': '#E6A23C',
        //     'background-color': '#E6A23C',
        //     'color': '#FFF'
        //   }
        // }
        return {
          'border-color': '#67C23A',
          'background-color': '#F0F9EB',
          'color': '#67C23A'
        }
      } else if (status == 2) {
        return {
          'border-color': '#F56C6B',
          'background-color': '#FEF0F0',
          'color': '#F56C6B'
        }
      } else if (status == 4 || status == 5) {
        return {
          'background-color': '#FFFFFF'
        }
      } else if (status == 6) {
        return {
          'border-color': '#E9E9EB',
          'background-color': '#F4F4F5',
          'color': '#909399'
        }
      }
    },
    getStatusName(row) {
      const status = row.checkStatus;
      const is_accept = row.isAccept;
      if (status == 0) {
        return '待审核'
      } else if (status == 1) {
        // if(is_accept == 1){
        //   return '已分配'
        // }else{
        //   return '待接受'
        // }
        return '通过'
      } else if (status == 2) {
        return '拒绝'
      } else if (status == 3) {
        return '审核中'
      } else if (status == 4) {
        return '撤回'
      } else if (status == 5) {
        return '未提交'
      } else if (status == 6) {
        return '已作废'
      }
      return ''
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../styles/relativecrm.scss';
</style>
